package com.zhentao.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.pojo.car.ChargeCarInfo;
import com.zhentao.service.ChargeCarInfoService;
import com.zhentao.mapper.ChargeCarInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author 田桑
* @description 针对表【charge_car_info(车辆信息表)】的数据库操作Service实现
* @createDate 2024-12-18 08:58:45
*/
@Service
public class ChargeCarInfoServiceImpl extends ServiceImpl<ChargeCarInfoMapper, ChargeCarInfo>
    implements ChargeCarInfoService{
    @Autowired
    ChargeCarInfoMapper chargeCarInfoMapper;

    @Override
    public ResponseResult startCharg(Integer id) {
        ChargeCarInfo cci = this.getById(id);

        // 电量已满
        if (cci.getElectromagnetic() == 100) {
            return ResponseResult.errorResult(400, "电量已满");
        }

        // 车辆正在充电中
        if (cci.getEStatu() == 2) {
            return ResponseResult.errorResult(400, "车辆正在充电中");
        }
        if (cci.getBill() != 0){
            return ResponseResult.errorResult(400, "您有未支付的账单");
        }

        // 满足条件开始充电
        if (cci.getElectromagnetic() != 100 && cci.getEStatu() == 1) {
            chargeCarInfoMapper.open();
            chargeCarInfoMapper.startCharg(id);
            ChargeCarInfo upcci = new ChargeCarInfo();
            upcci.setId(id);
            upcci.setEStatu(2);
            this.updateById(upcci);
            return ResponseResult.okResult(200,"开始充电");
        }

        // 默认返回，防止漏掉条件
        return ResponseResult.errorResult(400, "无法开始充电");
    }


    @Override
    public ResponseResult getByOwner(Integer userId) {
        //根据车主查询车辆
        return ResponseResult.okResult(chargeCarInfoMapper.getByOwner(userId));
    }

    @Override
    public ResponseResult stopCharg(Integer id) {
        ChargeCarInfo cci = this.getById(id);
        if(cci.getEStatu() == 1){
            return ResponseResult.okResult(400,"当前车辆没有在充电");
        }
        if(cci.getEStatu() == 2){
            chargeCarInfoMapper.delStartCharg(id);
            ChargeCarInfo upcci = new ChargeCarInfo();
            upcci.setId(id);
            upcci.setEStatu(1);
            this.updateById(upcci);
            return ResponseResult.okResult(200,"停止充电");
        }
        return ResponseResult.errorResult(400, "无法停止充电");
    }

    @Override
    public ResponseResult carList() {
        return ResponseResult.okResult(chargeCarInfoMapper.carList());
    }

    @Override
    public ResponseResult open() {
        return null;
    }

    @Override
    public ResponseResult payBill(Integer id) {
        return chargeCarInfoMapper.payBill(id);
    }
}




